<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ include file="/public/header.jsp"%>
<%
    String callback = request.getParameter("callback");
String c_s_id = request.getParameter("c_s_id") == null ? "" : request.getParameter("c_s_id");
String isShow = request.getParameter("isShow") == null ? "" : request.getParameter("isShow");
Map<String, Object> sessionMap = (Map<String, Object>) session.getAttribute("LOGIN_USER");
String person_id = String.valueOf(sessionMap.get("id"));
String person = String.valueOf(sessionMap.get("truename"));
boolean isAdd = "".equals(c_s_id);
int flag = 0;
if(!isAdd){
	flag = 1;
}
%>
<html>
	<head>
		<title>客户库</title>
		<script type="text/javascript" src="<%=contextPath %>/public/js/extend.js"></script>
		<style>
			.td1{
				padding-top:10px;
				padding-bottom:5px;
				font-size: 12px;
				background-color: #c0ebd7;
				width:20%;
			}
			.td2{
				padding:5px;
			}
		</style>
	<script type="text/javascript">
		var lastIndex;
		var customerId="";
		var customerName="";
		var warehouseId="";
		var warehouseName="";
		$(function(){
			<%if(isAdd){%>
			$("#operation_date").datetimebox('setValue',getTodaySecond());
			<%}else{%>
			jQuery('#ff').form({
				onLoadSuccess:function(data){
					 customerId=data.customer_id;
					 customerName=data.customer_name;
					 warehouseId=data.warehouse_id;
					 warehouseName=data.warehouse_name;
				}
			});
			$('#ff').form('load','<%=contextPath %>/springMVC/warehouse/getCustomerMaterialById?c_s_id=<%=c_s_id%>');
			<%}%>
			$("#warehouse_id").combogrid({
				title:'选择仓库',
				panelWidth:550,
				panelHeight:400,
				url:'<%=contextPath %>/springMVC/warehouse/listPageWareHouse?type=0',
				idField:"id",
				textField:"name",
				rownumbers:true,
				fitColumns:true,
				nowrap:false,
				columns: [[
					{field:'id',hidden:true},
					{field:'type',hidden:true},
					{field:'dept_name',title:'所属部门',width:100,align:'center'},
					{field:'name',title:'仓库名称',width:100,align:'center'}
				]],
				pageSize:10,
				pageList:[10,50,100],  
				pagination:true,
				toolbar:'#tb',
				onLoadSuccess:function(rtJson){
					jQuery('#warehouse_id').combogrid('setValue',warehouseId);
					jQuery('#warehouse_id').combogrid('setText',warehouseName);
				}
			});
			
			$("#cSearchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		 docSearch();
		       	} 
		   	}); 
			
			$("#type_id").combotree({
				url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=1',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					docSearch();
				}
			});
			$('#ctb').hide();
			
			$("#customer_id").combogrid({
				idField:"id",
				textField:"company",
				title:'选择客户',
				url: '<%=contextPath %>/springMVC/crm/listPageCustomer',
			    panelWidth:550,
			    panelHeight:400,
			    rownumbers:true,
			    nowrap:false,
			    fitColumns:true,
			    columns: [[  
			       {field:'id',title:'主键',hidden:true},  
			       {title:'客户类别',field:'type_name',width:200,align:'center'},
				   {title:'公司名称',field:'company',width:200,align:'center'},
				   {title:'联系人',field:'contact',width:200,align:'center'},
				   {title:'TEL',field:'tel',width:200,align:'center'}
			    ]],
			    toolbar:'#ctb',
			    pageSize:10,
			    pageList : [10,50,100],  
				pagination:true,
				onLoadSuccess:function(rtJson){
					jQuery('#customer_id').combogrid('setValue',customerId);
					jQuery('#customer_id').combogrid('setText',customerName);
				}
			});

			$("#mSearchValue").keypress(function(e) { 
		    	// 回车键事件 
		     	if(e.which == 13) { 
		     		 domSearch();
		       	} 
		   	}); 
			$("#material_type_id").combotree({
				url:'<%=contextPath %>/springMVC/group/getSystemTypeTree?type=0',
				method:'POST',
				animate:true,
				lines:true,
				onChange:function(){
					domSearch();
				}
			});
			$('#mtb').hide();
			
			$('#table').datagrid({
				<%=isAdd?"url:'',":"url:'"+contextPath+"/springMVC/warehouse/getCSMaterialByCsId?c_s_id="+c_s_id+"',"%>
				rownumbers:true,
				fitColumns:true,
				frozenColumns:[[
					{field:'ck',checkbox:true},
				    {title:'产品id',field:'product_id',hidden:true}
				]],
				columns:[[
					{title:'产品编号',field:'code',width:100,align:'center',editor:{
						type:'combogrid',
						options:{
							url: '<%=contextPath %>/springMVC/warehouse/listPageMaterial',
						    panelWidth:750,
						    panelHeight:355,
						    rownumbers:true,
						    fitColumns:true,
						    columns: [[  
						       {field:'id',title:'主键',hidden:true},  
						       {field:'material_type_name',title:'产品类别',width:100,align:'center'},
						       {field:'code',title:'产品编号',width:100,align:'center'},
						       {field:'name',title:'产品名称',width:150,align:'center'},
						       {field:'format_type',title:'规格/型号',width:100,align:'center'},
						       {field:'unit_name',title:'单位',width:50,align:'center'},
						       {field:'remark',title:'备注',width:200,align:'center'}
						    ]],
						    toolbar:'#mtb',
						    onBeforeLoad:function(param){
								var mSearchValue = $('#mSearchValue').val();
								var material_type_id = $('#material_type_id').combotree('getValue');
								param.searchValue=mSearchValue;
								param.material_type_id=material_type_id;
							},
						    pageList : [10,50,100],  
							pagination:true,
							onClickRow:function(rowIndex,rowData){
									var pRow = $('#table').datagrid('getRows')[lastIndex];
									pRow.product_id = rowData.id;
									pRow.code = rowData.code;
									pRow.name = rowData.name;
									pRow.unit_name = rowData.unit_name;
									pRow.format_type = rowData.format_type;
									$('#table').datagrid('getData').rows[lastIndex]=pRow;
									var data = $('#table').datagrid('getData');
									$('#table').datagrid('loadData',data);
									
									$('#table').datagrid('endEdit', lastIndex);
									$('#table').datagrid('beginEdit', lastIndex);
									//lastIndex =-1;
							}
						}
					}},
					{title:'产品名称',field:'name',width:200,align:'center'},
					{title:'规格/型号',field:'format_type',width:200,align:'center'},
					{title:'单位',field:'unit_name',width:50,align:'center'},
					{title:'录入数量',field:'num',width:100,align:'center',editor:{
						type:'numberbox',
						options:{
							precision:2
						}
					}},
					{title:'备注',field:'remark',width:260,align:'center',editor:{
						type:'textarea'
					}}
				]],
				onBeforeLoad:function(){
					$(this).datagrid('rejectChanges');
				},
				onClickCell:function(rowIndex,field,value){
					if (lastIndex != rowIndex){
						$('#table').datagrid('endEdit', lastIndex);
						$('#table').datagrid('beginEdit', rowIndex);
						var ed = $('#table').datagrid('getEditor', {index:rowIndex,field:field});
						if(ed == null){
							ed = $('#table').datagrid('getEditor', {index:rowIndex,field:'cost'});
						}
						$(ed.target).select();
					}
					lastIndex = rowIndex;
				},
				toolbar:[
					{
						id:'add',
						text:'选择产品',
						disabled:false,
						iconCls:'icon-add',
						handler:function(){
							addRow();
						}
					},'-',{
					id:'save',
					text:'保存修改',
					disabled:false,
					iconCls:'icon-save',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
					}
					},'-',{
					id:'delete',
					text:'删除',
					disabled:false,
					iconCls:'icon-remove',
					handler:function(){
						$('#table').datagrid('acceptChanges');
						lastIndex =-1;
						var rows = $('#table').datagrid('getSelections');
						if (rows.length > 0){
							for(var i=0;i<rows.length;i++){
								var row = rows[i];
								var index = $('#table').datagrid('getRowIndex', row);
								$('#table').datagrid('deleteRow', index);
							}
						}
						var data = $('#table').datagrid('getData');
						$('#table').datagrid('loadData',data);
					}
				}
			   ]
			});
			
		});
		function searchDepart(){
			var dept_id =  $("#dept_id").combotree('getValue');
			$('#warehouse').combogrid('clear');
			$('#warehouse').combogrid("grid").datagrid('load',{
				dept_id:dept_id
			});
		}
		function docSearch()
		{
			var cSearchValue = $('#cSearchValue').val();
			var type_id = $('#type_id').combotree('getValue');
			$('#customer_id').combogrid('clear');
			$('#customer_id').combogrid('grid').datagrid('load',{
				searchValue:cSearchValue,
				type_id:type_id
				});
		}
		function domSearch()
		{
			var mSearchValue = $('#mSearchValue').val();
			var material_type_id = $('#material_type_id').combotree('getValue');
			$(combogridTarget).combogrid('grid').datagrid('load',{
				searchValue:mSearchValue,
				material_type_id:material_type_id
				});
		}
		function addRow(){
			$('#table').datagrid('endEdit', lastIndex);
			$('#table').datagrid('appendRow',{
				top:'1000',
				low:'0',
				num:'0'
			});
			var data = $('#table').datagrid('getData');
			$('#table').datagrid('loadData',data);
			lastIndex = $('#table').datagrid('getRows').length-1;
			$('#table').datagrid('selectRow', lastIndex);
			$('#table').datagrid('beginEdit', lastIndex);
		}
		
		function addDB(obj){
			//自动提交数据
			$('#table').datagrid('acceptChanges');
			lastIndex =-1;
			//自动提交数据
			
			var warehouse_id = $("#warehouse_id").combogrid("getValue");
			if(!checkNullOREmpty(warehouse_id,'仓库为必填项')){return;}
			
			var customer_id = $("#customer_id").combogrid("getValue");
			if(!checkNullOREmpty(customer_id,'客户为必填项')){return;}
			
			var products="";
			var jsondata=$("#table").datagrid("getData");
			for(var i=0;i<jsondata.total;i++){
				if(products!=""){
					products+=";";
				}
				products+=jsonToStr(jsondata.rows[i]);
				var index = $("#table").datagrid("getRowIndex",jsondata.rows[i])+1;
				if(jsondata.rows[i].name == null || jsondata.rows[i].name == ""){
					$.messager.alert('提示','第'+index+'行未选择产品！');
					return ;
				}else if(jsondata.rows[i].num == null || jsondata.rows[i].num == "" || jsondata.rows[i].num <= 0){
					$.messager.alert('提示','第'+index+'行数量填写不正确！');
					return ;
				}
			}
			if(!checkNullOREmpty(products,'产品为必填项')){return;}
			var remark=$('#remark').val();
			var in_code=$('#in_code').val();
			
			var param = "in_code="+in_code+"&operation_date="+$('#operation_date').datetimebox('getValue')+"&remark="+remark+"&warehouse_id="+warehouse_id+"&customer_id="+customer_id+"&products="+products+"&person_id=<%=person_id%>&person_name=<%=person%>&flagType=0";
			$(obj).hide();
			$.ajax({
				type:'POST',
				<%=isAdd?"url:'"+contextPath+"/springMVC/warehouse/addCustomerMaterial',":"url:'"+contextPath+"/springMVC/warehouse/updateCustomerMaterial?c_s_id="+c_s_id+"',"%>
				data:param,
				dataType:'json',
				success:function(rtJson){
					if(rtJson.rtState == '0'){
						eval('window.opener.<%=callback%>();');
						window.close();
					}
					else{
						$.messager.alert("提示",rtJson.rtMsrg);
					}
					$(obj).show();
				}
			});
		}
		function gdPrint(){
			openDialogResize("<%=contextPath%>/springViews/print/startCustomerMaterialPrint.jsp?c_s_id=<%=c_s_id%>",1000, 800);
		}
		function addOrEditMaterial(){
				openDialogResize("<%=contextPath %>/springViews/warehouse/addOrEditMaterial.jsp?callback=rtCallBack",1000, 800);
		}
		function rtCallBack(){
		}
	</script>
	</head>
	<body>
		<div style="width: 90%;padding-left:50px">
		<form id="ff">
			<table  style="border-collapse: collapse;" width="100%" border="1px" bordercolor="#0eb83a">
				<tr>
					<td align="right" class="td1" nowrap>入库单号：</td>
					<td class="td2" nowrap  > 
						<input  id="in_code" name="in_code"  class="easyui-validatebox" data-options="required:true">
					</td>
					<td align="right" class="td1" nowrap>选择仓库：</td>
					<td class="td2" nowrap  > 
						<input  id="warehouse_id" name="warehouse_id" >
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>选择客户：</td>
					<td class="td2" nowrap >
						<input  id="customer_id" name="customer_id" >
					</td>
					<td align="right" class="td1" nowrap>申请人：</td>
					<td class="td2" nowrap>
						<input  id="person_name" name="person_name" readOnly value="<%=person%>">
						<input  type="hidden" id="person_id" name="person_id"  value="<%=person_id%>">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>日期：</td>
					<td class="td2" nowrap  colspan="3">
						<input  id="operation_date" name="operation_date" class="easyui-datetimebox" data-options="required:true">
					</td>
				</tr>
				<tr>
					<td align="right" class="td1" nowrap>摘要：</td>
					<td class="td2" nowrap colspan='3'>
						<textarea  id="remark" name="remark" cols="100" ></textarea>
					</td>
				</tr>
				<tr>
					<td colspan="4"  nowrap>
						<div id="table"></div>
					</td>
				</tr>
				<%if(!"1".equals(isShow)){%>
				<tr>
					<td  nowrap colspan="4">
						<div align="center">
							
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-save',plain:false" onclick="addDB(this);">提交</a>
							
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-cancel',plain:false" onclick="javascript:window.close();">关闭</a>
						</div>
					</td>
					</tr>
					<%} else{%>
				<tr>
					<td align="right" class="td1" nowrap></td>
					<td class="td2" nowrap colspan='4'>
						<div align="left">
							<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-print',plain:false" onclick="gdPrint();">打印预览</a>
						</div>
					</td>
				</tr>
			<%}%>
			</table>
			</form>
		</div>
		
		<div id="ctb" style="padding:3px;height:auto;">
			<div align="right">
				分类：<input  id="type_id" name="type_id" >
				查询条件：<input id="cSearchValue" name="cSearchValue"  title="客户名称或联系人"/>
				<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="docSearch();"></a>
			</div>
		</div>
		
			<div id="mtb" style="padding:3px;height:auto">
				<div style="float:left;">
					<a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="addOrEditMaterial();">新增材料</a>
				</div>
				<div align="right">
					分类：<input  id="material_type_id" name="material_type_id" class="easyui-combotree">
					查询条件：<input id="mSearchValue" name="mSearchValue" style="width:150px" title="材料编码或名称"/>
					<a href="javascript:void(0)" class="easyui-linkbutton" data-options="iconCls:'icon-search',plain:true" onclick="domSearch();"></a>
				</div>
			</div>
		
	</body>
</html>